<?php
class Iupload{
	function index(){
		$posturl="http://www.imageshack.us/upload_api.php";

		$fileupload = $_FILES["img"]['tmp_name'];

		$isIframe =($_POST["iframe"]) ? true:false;
		$idarea = $_POST["idarea"];
		$maxwidth = is_numeric($_POST["maxwidth"]) ? $_POST["maxwidth"]:600;
		$maxheight = is_numeric($_POST["maxheight"]) ? $_POST["maxheight"]:600;
		
		$fileupload=@$_FILES['img']['name'];
		$fileupload_tmp=@$_FILES['img']['tmp_name'];
		if($fileupload != ''){
			$file = "uploads/".$fileupload;
			$res = move_uploaded_file($fileupload_tmp, $file);
		}
		
		if ($isIframe) {
			echo '<html><body>OK<script>window.parent.$("#'.$idarea.'").insertImage("'.$base_url.'uploads/'.$fileupload.'","'.$base_url.'uploads/'.$fileupload.'").closeModal().updateUI();</script></body></html>';
		}else{
			header("Content-type: text/javascript");
			if (!$res) {
				echo '{"status":0,"msg":"Error"}';
			}else{
				echo '{"status":1,"msg":"OK","image_link":"'.$base_url.'uploads/'.$fileupload.'","thumb_link":"'.$base_url.'uploads/'.$fileupload.'"}';
			}
		}

		/* $postData = array(
			'fileupload' => "@".$fileupload,
			'key'        => '5ACEJKLT7a388c7eb69f1547742d4fc639f1adc1', // по умолчанию ключ WysiBB, рекомедуется использовать свой ключ полученный на странице http://stream.imageshack.us/xmlapi/
			'optimage'	 => 1,
			'optsize'	 =>	$maxwidth."x".$maxheight,
			'xml'        => 'yes'
		);

		$ch = curl_init($posturl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
		$result = curl_exec($ch);
		curl_close($ch);
		 
		$xml = simplexml_load_string($result); */

		
	}
}